var mongoose = require('mongoose'),
		Schema = mongoose.Schema;




/* * * * * * *
 *
 * Schemas
 *
 * * * * * * */
var teacherSchema = new mongoose.Schema({           
			_id:{type:String,required:true},                                           //教师登录id，唯一标识
			tname:{type:String,required:true},                                         
			passwd:{type:String,required:true},                                        
			course:[{_id:String,
							 cname:String,
							 del:{type:Boolean,
							 			default:false}}]                                         //教师教授的课程，单向查询
		}),                                                                       
		studentSchema = new mongoose.Schema({                                        
			_id:{type:String,required:true},                                           //学号
			sname:{type:String,required:true},                                      
			passwd:{type:String},                                                   
			sclass:{type:String}                                                       //班级
		}),                                                                               
		courseSchema = new mongoose.Schema({                                              
			cname:{type:String,required:true},                                         
			random:{type:Number,required:true},                                        //生成激活码的种子，以前的版本有用
			signup_times:{type:Number,default:0},                                      //已经签了几次到了
			time_each:[{                                                               //第几次签到的时间（如果有时间的话），这样做也是历史遗留问题
				times:Number,                                                            
				time :Date                                                               
			}],                                                                        
			maxStudent:Number,                                                         //历史遗留
			CourseTimme:[{                                                             //尚未实现的功能
				beginWeek:{type:Number,max:24,min:1},           
				endWeek:{type:Number,max:24,min:1},             
				weekDay:{type:Number,max:5,min:1},
				dayTime:{type:Number,max:11,min:1}
			}]
		}),
		SCSchema = new mongoose.Schema({                                             //选课表，建立学生和课的关系
			student:{type:String,ref:'Student',required:true},                         
			course:{type:Schema.Types.ObjectId,ref:'Course',required:true},          
			signup:[{time:Number,_id:String}],                                         //第几次签到了，用于签到的key是什么
			nextIll:{time:Number,MD:String,logd:Boolean}
		}),
		keySchema = new mongoose.Schema({
			_id:{type:String,required:true},                                           //key唯一标识
			cid:{type:Schema.Types.ObjectId,ref:'Course'},                             //key绑定的课程
			time:{type:Number},                                                        //第几次
			Tmp:String,                                                                //key对应的临时激活码
			signs:String                                                               //临时签到的学生
		}),
		examInfoSchema = new mongoose.Schema({                                       //尚未实现的功能
			subject:{type:String,required:true},
			time:{mouth:{type:Number,required:true},
				day:{type:Number,required:true},
				range:{
					begin:{hour:{type:Number,max:23,min:0},minute:{type:Number,max:59,min:0}},
					end:{hour:{type:Number,max:23,min:0},minute:{type:Number,max:59,min:0}}
				}},
			place:String,
			teacher:[String],
			poster:{type:Schema.Types.ObjectId,ref:'Teacher'}
		});

/* * * * * * *
 *
 * indexes
 *
 * * * * * * */

SCSchema.index({"student":1,"course":1},{unique:true,name:"SCindex"});

/* * * * * * *
 *
 * models
 *
 * * * * * * */

var Teacher = mongoose.model('Teacher',teacherSchema),
		Student = mongoose.model('Student',studentSchema),
		Course  = mongoose.model('Course',courseSchema),
		SC      = mongoose.model('SC',SCSchema),
		Key     = mongoose.model('Key',keySchema),
		ExamInfo = mongoose.model('ExamInfo',examInfoSchema);



module.exports = {
	Student:Student,
	Teacher:Teacher,
	Course:Course,
	Key:Key,
	SC:SC,
	SCSchema:SCSchema
};
